#!/bin/bash
if [ ! -s result.txt ]; then
    exit 0
fi
video_path=`date +%Y-%m-%d_%H-%M-%S`
target_dir=/sdcard/Download/video_${video_path}
mkdir -p $target_dir
#wget2 -N --max-threads=20 -i   result.txt -P /sdcard/Download/video_${video_path}
wget -i   result.txt -P /sdcard/Download/video_${video_path} -T 30 -t 3s

cd /sdcard/Download/video_${video_path}
if [ -n "$(ls -A)" ]; then
    echo aa
else
    echo /sdcard/Download/video_${video_path} is empty
    exit 0
fi
ls | awk -F '?' '{print $0,$1}' | xargs -n 2 mv
cd -


# 按修改时间排序并处理文件
find "$target_dir" -maxdepth 1 -type f -print0 | \
xargs -0 ls -t | \
while read -r file; do
    # 获取文件信息
    filename=$(basename "$file")
    extension="${filename##*.}"
    mtime=$video_path
    
    # 构建新文件名
    new_name="${mtime}_$(printf "%03d" $counter).${extension}"
    
    # 执行重命名（先输出预览）
    echo "Renaming: $filename -> $new_name"
    mv "$file" "$target_dir/$new_name"
    
    ((counter++))
done

echo "Renaming completed. Total files processed: $((counter-1))"